package ro.pub.cs.stadium.document.logic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

import ro.pub.cs.stadium.document.AbstractState;
import ro.pub.cs.stadium.document.IDocument;
import ro.pub.cs.stadium.document.IState;
import ro.pub.cs.stadium.document.ITransition;
import ro.pub.cs.stadium.document.Point;

public class State extends AbstractState {

	ArrayList<String> actions;
	VariableList outVars;
	Map<Variable, Boolean> values;

	public State(String name,VariableList curent) {
		actions = new ArrayList<String>();
		setName(name);
		setAttribute("type", "state");
		outVars = new VariableList();
	}

	public void setActions(String s) throws ExpressionException {
		values = new HashMap<Variable, Boolean>();
		StringTokenizer st = new StringTokenizer(s,";");
		while (st.hasMoreTokens()) {
			String linie = st.nextToken();
			actions.add(linie);
			outVars.addVariable(parseVar(linie));
		}
	}
	
	public String getAllActions() {
		StringBuffer result = new StringBuffer();
		if (!actions.isEmpty()) {
			for (String s : actions) {
				result.append(s + "; ");
			}
		}
		else
			result.append("n/a");
		return result.toString();
	}
	
	public ArrayList<String> getActions() {
		return actions;
	}
	
	public Map<Variable,Boolean> getValues() {
		return values;
	}
	
	public VariableList getVariables() {
		return outVars;
	}

	private Variable parseVar(String linie) throws ExpressionException{
		StringTokenizer st = new StringTokenizer(linie," =");
		if (st.countTokens()<2) throw new ExpressionException("error in assignment");
		Variable V = new Variable(st.nextToken());
		V.setType(true);
		int value = Integer.parseInt( st.nextToken());
		switch (value) {
			case 0: values.put(V, false); break;
			case 1: values.put(V, true); break;
			default: throw new ExpressionException("error in assignment");
		}
		return V;
	}
	
	public static void main(String args[]) {
		try {
			VariableList V = new VariableList();
			V.addVariable(new Variable("A",true));
			V.addVariable(new Variable("C",true));
		State S = new State("S1",V);
		S.setActions("A = 0; B = 1; V = 0; Y = 1;");
		System.out.println(S.getVariables());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
